// components/personCenter/personCenter.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    userId: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    _ip: 'http://www.mmdccj.top:9003',
    userName: null,
    introduce: null,
    avatar: null,
    recordInfo: [],
    playCount: 0
    
  },

  /**
   * 组件的方法列表
   */
  methods: {
    update: function (e) {
      this.triggerEvent('update')
    },
    addToListAndPlay: function (e) {
      const info = e.currentTarget.dataset.info
      console.log(info)
      app.setMusicAndPlay(info.music, info.user.name)
      this.update()
    },
  },
  lifetimes: {
    attached: function () {
      console.log(this.properties.userId)
      console.log('进入个人中心')
      wx.request({
        url: this.data._ip + '/user/getPublicInfo',
        method: 'GET',
        data: {
          userId: this.properties.userId
          // userId: '1'
        },
        success: res => {
          console.log(res.data.data)
          if (res.data.code != 1) return
          this.setData({
            avatar: res.data.data.avatar,
            userName: res.data.data.name,
            introduce: res.data.data.introduce
          })
        },
        fail: res => {
          console.log(res)
        }
      })
      // this.setData({
      //   recordInfo: null,
      //   playCount: 0
      // })
      wx.request({
        url: this.data._ip + '/music/getWeekPlay',
        method: 'GET',
        data: {
          // id: '1'
          id: this.properties.userId
        },
        success: res => {
          console.log(res)
          if (res.data.code != 1) return
          let playCount = 0;
          for (let i = 0; i < res.data.data.length; i++) {
            playCount += res.data.data[i].count
          }
          console.log(playCount)
          this.setData({
            recordInfo: res.data.data,
            playCount: playCount
          })
        },
        fail: res => {
          console.log(res)
        }
      })
    },

  }
})
